<h1 id="portability">Portability</h1>

<h2>Platforms and compilers</h2>

<p>The table below summarizes the platforms and compilers which Duktape is known
to work on, with portability notes where appropriate.  This is <b>not an exhaustive
list</b> of supported/unsupported platforms, rather a list of what is known to work
(and not to work).  Platform and compiler specific issues are discussed in more
detail below the table.</p>

<table>
<thead>
<tr>
<th>Operating system</th>
<th>Compiler</th>
<th>Processor</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Linux</td>
<td>GCC</td>
<td>x86</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>GCC</td>
<td>x64</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>GCC</td>
<td>x32</td>
<td>No known issues, use <code>-mx32</code>.</td>
</tr>
<tr>
<td>Linux</td>
<td>GCC</td>
<td>ARM</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>GCC</td>
<td>MIPS</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>GCC</td>
<td>SuperH</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>GCC</td>
<td>SPARC</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>Clang</td>
<td>x86</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>Clang</td>
<td>x64</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>Clang</td>
<td>ARM</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>Clang</td>
<td>MIPS</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>TCC</td>
<td>x64</td>
<td>Zero sign issues (see below).</td>
</tr>
<tr>
<td>FreeBSD</td>
<td>Clang</td>
<td>x86</td>
<td>Aliasing issues with clang 3.3 on 64-bit FreeBSD, <code>-m32</code>, and packed <code>duk_tval</code> (see below).</td>
</tr>
<tr>
<td>FreeBSD</td>
<td>Clang</td>
<td>x64</td>
<td>No known issues.</td>
</tr>
<tr>
<td>NetBSD</td>
<td>GCC</td>
<td>x86</td>
<td>No known issues (NetBSD 6.0).  There are some <code>pow()</code> function incompatibilities on NetBSD, but
    there is a workaround for them.</td>
</tr>
<tr>
<td>OpenBSD</td>
<td>GCC</td>
<td>x86</td>
<td>No known issues (OpenBSD 5.4).</td>
</tr>
<tr>
<td>Windows</td>
<td>MinGW</td>
<td>x86</td>
<td><code>-std=c99</code> recommended, only ISO 8601 date format supported (no platform specific format).</td>
</tr>
<tr>
<td>Windows</td>
<td>MinGW-w64</td>
<td>x64</td>
<td><code>-m64</code>, <code>-std=c99</code> recommended, only ISO 8601 date format supported (no platform specific format).</td>
</tr>
<tr>
<td>Windows</td>
<td>MSVC<br />(Visual Studio Express 2010)</td>
<td>x86</td>
<td>Only ISO 8601 date format supported (no platform specific format).
    Harmless warnings if <code>/Wp64</code> is enabled.</td>
</tr>
<tr>
<td>Windows</td>
<td>MSVC<br />(Visual Studio Express 2013 for Windows Desktop)</td>
<td>x64</td>
<td>Only ISO 8601 date format supported (no platform specific format).</td>
</tr>
<tr>
<td>Windows</td>
<td>MSVC<br />(Visual Studio 2010)</td>
<td>x64</td>
<td>Only ISO 8601 date format supported (no platform specific format).
    May require explicit <code>DUK_OPT_NO_PACKED_TVAL</code> with
    Duktape 0.10.0.</td>
</tr>
<tr>
<td>Android</td>
<td>GCC<br />(Android NDK)</td>
<td>ARM</td>
<td><code>-std=c99</code> required at least on some NDK versions.</td>
</tr>
<tr>
<td>OSX</td>
<td>Clang</td>
<td>x64</td>
<td>Tested on OSX 10.9.2 with XCode.</td>
</tr>
<tr>
<td>Darwin</td>
<td>GCC</td>
<td>x86</td>
<td>No known issues.</td>
</tr>
<tr>
<td>QNX</td>
<td>GCC</td>
<td>x86</td>
<td><code>-std=c99</code> recommended.  Architectures other than x86 should also work.</td>
</tr>
<tr>
<td>AmigaOS</td>
<td>VBCC</td>
<td>M68K</td>
<td>Requires some preprocessor defines, datetime resolution limited to full seconds.</td>
</tr>
<tr>
<td>TOS<br />(Atari ST)</td>
<td>VBCC</td>
<td>M68K</td>
<td>Requires some preprocessor defines, datetime resolution limited to full seconds.</td>
</tr>
<tr>
<td>Emscripten</td>
<td>Emscripten</td>
<td>n/a</td>
<td>Requires additional options, see below. At least V8/NodeJs works.</td>
</tr>
<tr>
<td>Adobe Flash Runtime</td>
<td>CrossBridge<br />(GCC-4.2 with Flash backend)</td>
<td>n/a</td>
<td><code>-std=c99</code> recommended, may need <code>-jvmopt=-Xmx1G</code> if running
    32-bit Java.  Tested with <a href="http://adobe-flash.github.io/crossbridge/">CrossBridge</a>
    1.0.1 on 64-bit Windows 7.</td>
</tr>
<tr>
<td>pNaCl</td>
<td>clang</td>
<td>n/a</td>
<td>No known issues.</td>
</tr>
<tr>
<td>Linux</td>
<td>BCC<br />(Bruce's C compiler)</td>
<td>i386</td>
<td><code>-3</code> and <code>-ansi</code> required; compiles but doesn't link.  This is an
    old compiler useful for portability torture tests (for instance 16-bit <code>int</code> type).</td>
</tr>
</tbody>
</table>

<h3>Clang</h3>

<p>Clang 3.3 on FreeBSD has some aliasing issues (at least) when using
<code>-m32</code> and when Duktape ends up using a packed <code>duk_tval</code>
value representation type.  You can work around the problem by defining
<code>DUK_OPT_NO_PACKED_TVAL</code> to disable packed value type.  The
problem does not appear in all clang versions.  Duktape self tests cover
this issue (define <code>DUK_OPT_SELF_TESTS</code> when compiling).
See internal test file <code>misc/clang_aliasing.c</code>.</p>

<h3>MSVC</h3>

<p>The <a href="http://msdn.microsoft.com/en-us/library/yt4xw8fh.aspx"><code>/Wp64</code>
(Detect 64-bit Portability issues)</a> option causes harmless compile warnings when compiling
32-bit code, e.g.:</p>
<pre>
duk_api.c(2419): warning C4311: 'type cast' : pointer truncation from 'duk_hstring *' to 'duk_uint32_t'
</pre>

<p>The warnings are caused by Duktape casting 32-bit pointers to 32-bit integers
used by its internal value representation.  These casts would be incorrect in a 64-bit
environment which is reported by the <code>/Wp64</code> option.  When Duktape is
compiled in a 64-bit environment a different value representation is used which
doesn't use these casts at all, so the warnings are not relevant.</p>

<p>Compilation with <code>/Wall</code> is not clean at the moment.</p>

<h3>TCC</h3>

<p>TCC has zero sign handling issues; Duktape mostly works but zero sign is
not handled correctly.  This results in Ecmascript non-compliance, for
instance <code>1/-0</code> evaluates to <code>Infinity</code>, not <code>-Infinity</code>
as it should.</p>

<h3>VBCC (AmigaOS / TOS)</h3>

<p>VBCC doesn't appear to provide OS or processor defines.  To compile for
M68K AmigaOS or TOS you must:</p>
<ul>
<li>Define <code>__MC68K__</code> manually.</li>
<li>Define either <code>AMIGA</code> or <code>__TOS__</code> manually.</li>
</ul>

<p>Datetime resolution is limited to full seconds only when using VBCC on
AmigaOS or TOS.</p>

<h3>Emscripten</h3>

<p>Needs a set of <code>emcc</code> options.  When executed with
V8, the following seem to work:</p>
<ul>
<li><code>-DEMSCRIPTEN</code>: <b>mandatory option</b>, needed by Duktape
    to detect Emscripten.  Without this Duktape may use unaligned accesses
    which Emscripten does not allow.  This results in odd and inconsistent
    behavior, and is not necessarily caught by Duktape self tests.</li>
<li><code>-std=c99</code></li>
<li><code>-O2</code></li>
<li><code>--memory-init-file 0</code></li>
</ul>

<p>Dukweb is compiled using Emscripten, so you can also check out the Duktape
git repository to see how Dukweb is compiled.</p>

<h2>Limitations</h2>

<ul>
<li>Pointer less-than/greater-than comparisons are expected to work like
    pointers were unsigned.  This is incorrect on some platforms.</li>
<li><a href="http://en.wikipedia.org/wiki/Two's_complement">Two's complement</a>
    signed arithmetic is required.  This is not technically guaranteed by ANSI C,
    but there are very few environments where this assumption does not hold.</li>
</ul>

<h2>Troubleshooting</h2>

<ul>
<li>Compile in C mode if possible.  Although C++ compilation now works,
    it isn't as portable as C compilation.</li>
<li>Enable C99 mode if possible (<code>-std=c99</code> or similar).  Type
    detection without C99 is less reliable than with C99.</li>
<li>If Duktape compiles but doesn't seem to work correctly, enable
    self tests with <code>DUK_OPT_SELF_TESTS</code>.  Self tests
    detect some compiler and platform issues which cannot be caught
    compile time.</li>
<li>If the target platform has specific alignment requirements and Duktape
    doesn't autodetect the platform correctly, you may need to provide
    either <code>DUK_OPT_FORCE_ALIGN=4</code> or <code>DUK_OPT_FORCE_ALIGN=8</code>.
    The alignment number should match whatever alignment is needed for IEEE
    doubles and 64-bit integer values.</li>
<li>If compilation fails in endianness detection, Duktape probably doesn't
    (yet) support the platform specific endianness headers of your platform.
    Such headers are unfortunately non-standardized, so endianness detection
    is a common (and usually trivial) portability issue on custom platforms.
    Use <code>DUK_OPT_FORCE_BYTEORDER</code> to force endianness as a workaround.
    If you know how the endianness detection should work on your platform,
    please send an e-mail about the issue or contribute a patch.</li>
<li>Another typical portability issue on new/exotic platforms is the Date
    built-in, which requires a few platform specific functions for dealing
    with date and time.  Often existing Date functions are sufficient but
    if they're not, you can implement an external "Date provider" which
    requires no Duktape changes, see:
    <a href="https://github.com/svaarala/duktape/blob/master/doc/datetime.rst">datetime.rst</a>.</li>
</ul>

